<%= start_form_tag({:action => '#',:controller => '#'},{:id => 'schedule_time_form'})%>
<div style="float:right; margin-right:20px; font-size:10pt;">
<label> Show </label><%= select_tag 'days',options_for_select([['1 day',1]]+(2..7).map{|x|[x.to_s+' days',x]},session[:schedule][:days])%> 
&nbsp;&nbsp;
<a href="#" onclick="new Ajax.Updater('master_schedule_wrap', '/schedule/master_schedule?date=<%=@date+session[:schedule][:days]%>&days=<%=session[:schedule][:days]%>&master=<%=@master_schedule%>', {asynchronous:true, evalScripts:true});"> >></a>
</div>
<div style=" font-size:10pt; float:left; margin-left:20px;"> 

<a href="#" onclick="new Ajax.Updater('master_schedule_wrap', '/schedule/master_schedule?date=<%=@date-session[:schedule][:days]%>&days=<%=session[:schedule][:days]%>&master=<%=@master_schedule%>', {asynchronous:true, evalScripts:true});"><< </a>
&nbsp;&nbsp;
<label>Start From:</label> 
	
	<input name="date" id="date" value="<%=@date%>" style="display:none"/>
	<input id="trigger" type="submit" value="<%=@date%>" onclick="return false;">
	
	<div style="padding-left:10px; display:inline;">
	<%= link_to "Print Schedule", :action => "printable_schedule",:year => @from.year, :month => @from.month, :day => @from.day %>
	</div>

<% if session[:filter_schedule]=='false' %>
<a href="#" onclick="new Ajax.Updater('master_schedule_wrap', '/schedule/master_schedule?filter=true&'+Form.serialize('schedule_time_form')+'&master=<%=@master_schedule%>', {asynchronous:true, evalScripts:true});">Show <%=current_user.default_office.name%> Only</a>
<% else %>
<a href="#" onclick="new Ajax.Updater('master_schedule_wrap', '/schedule/master_schedule?filter=false&'+Form.serialize('schedule_time_form')+'&master=<%=@master_schedule%>', {asynchronous:true, evalScripts:true});">Show All Offices</a>
<% end %>
</div>
<%= end_form_tag %>
<div style="height:70px"></div>

<%= observe_form('schedule_time_form',:url =>{:action => 'master_schedule',:controller=>'schedule', :master => @master_schedule},:update =>'master_schedule_wrap')%>

<script type="text/javascript">
	Droppables.drops = []
</script>


<%
   msie_width_fix = isMSIE? ? 0: -2;
   msie_pos_fix = isMSIE? ? 1:1;
   msie_height_fix = isMSIE? ? 1:0;

   @days = ((@to-@from)/(60*60*24)).floor
   air_height = 20
   type_height = 17
   graph_height = @aircraft_count*air_height+(@types.length+1)*type_height+@instructors.length*air_height 
   graph_width = @master_schedule ? 1200 : 685
   label_width = 100.0
   tops = {}
   offset = 0
   width_per_second = (graph_width-label_width-1)/(@days*16*60*60)
   width_per_day = (graph_width-label_width-1)/(@days)
   width_per_hour = width_per_second*60*60

   @fixfrom = @from+7*60*60
   @fixto = @to-60*60
   %>
   
<!-- main schedule painting area -->
<div id="graph" 
style="position:relative; text-align:left; left:-6px; width:<%=graph_width%>px;max-width:100%;height:<%=graph_height%>px">

<!-- vertical background bars -->

<% (0..@days-1).each{|x|%>
	<div class="vbar" id="vbar1<%=x%>" 
    style="top:-35px; height:20px; width:<%=width_per_day+2%>px;
           left:<%=label_width+x*width_per_day%>px; 
			border-left: 1px solid black;
     		border-top: 1px solid black;
			text-align: center;
           ">
    </div>
   <div class="vbar" id="vbar1<%=x%>" 
    style="top:-15px; height:<%=graph_height+14%>px; width:<%=width_per_hour*2+2%>px;
           left:<%=label_width+x*width_per_day%>px; background:#f0f5ff;
			border-left:1px solid black;
     		text-align: left;
           ">
    </div>
	<% (1..7).each{|i| %>
	   <div class="vbar" id="vbar3<%=x%>" 
	    style="top:-15px; height:<%=graph_height+14%>px; width:<%=width_per_hour*2+2%>px;
	           left:<%=label_width+x*width_per_day+width_per_hour*2*i%>px; background:<%= i%2==1 ? "#d0e5ff" : "#f0f5ff"%>;
	     		text-align: left;
	           ">
	    </div>
	<%	} %>
    
<% } %>

<% can_see_names =  (can_approve_reservations? || instructor? || @is_admin) %>
<!-- aircraft section -->
	<% @types.each{|type| %>
	    <div id="over<%=type.id%>" class="type_label_over" style="top:<%=offset%>px; width:<%=graph_width%>px">
					<div style="float:right;"><%= link_to_remote 'Hide',
					{:url=>{:controller=>'schedule',:action => 'hide_type',:type_id=>type.id,:master=>@master_schedule},:update=>'master_schedule_wrap'},
					{:style=>'color:white;'}%></div>
	        <div class="shift_right"><%= type.type_name %></div>
	    </div>
	    <% offset += type_height %>
		<% @aircrafts.each{|aircraft| %>
		    <% if aircraft.type.id != type.id 
	    	       next
		       end %>
		    <div id="aircraft<%=aircraft.id%>" style="border-bottom: 1px solid black; height:<%=isMSIE? && !@master_schedule ? 20 : 14%>px;width:<%=graph_width%>px; top:<%=offset%>px" 
		         class="aircraft_name"><!--[<%=aircraft.default_office.name[0..0]%>] <%= aircraft.identifier%>-->
		    </div>
		    <% if can_approve_reservations?%>
			   	<script type="text/javascript">
					Droppables.add('aircraft<%=aircraft.id%>', {
				    onDrop: function(element) 
				     { 
						element.style.display = "none";
						new Ajax.Updater('master_schedule_wrap', '/schedule/drag_drop_reservation?master=<%=@master_schedule%>&sdate=<%=@from.to_date.to_s%>&drag=' + element.id + '&drop=a<%=aircraft.id%>', {asynchronous:true, evalScripts:true});           
					 }});
				</script>
            <% end %>			
			<%= drop_receiving_element("aircraft"+aircraft.id.to_s, :url =>
			    { :controller => "schedule", :action => "drag_drop_reservation" }) %>
	         <% tops["a#{aircraft.id}"] = offset %>
		     <% offset += air_height %> 
		<% } %>
	<% } %>
	
<!-- instructor section -->
	<div style="top:<%=offset%>px;width:<%=graph_width%>px"  
	   class="type_label_over">
	<div class="shift_right">Instructors</div></div>
	
    <% offset += type_height %>
	 <% @instructors.each{|instructor| %>
		    <div id="instructor<%=instructor.id%>" style="border-bottom: 1px solid black;height:14px; width:<%=graph_width%>px; top:<%=offset%>px" class="aircraft_name">
		    </div>
             <% tops["i#{instructor.id}"] = offset %>
		    <% offset += air_height %>
		<% } %>
		
        <% if @master_schedule%>        
        <script type="text/javascript">
             function autoposition_titles(){
                $("top_bar").style.top = "-32px";
                sc = getPageScroll()
                $("left_bar").style.left = sc[0]+"px";
                if(sc[1]>80){
                    $("top_bar").style.top = sc[1]-120+"px";                    
                }
             }
    
            if (window.addEventListener) {
                window.addEventListener("scroll", function (e) { autoposition_titles(); }, false);
            } else if (window.attachEvent) {
	            window.attachEvent('on' + "scroll", function (e) { autoposition_titles(); });
            }
        </script>
        <% end %>
<!-- reservation bars -->   
    <% @aircraft_reservations.each{|r|%>
		<% if tops["a#{r.aircraft_id}"].nil? then next end %>
   		<div id="abar<%=r.id%>" class="reservation_bar" 
		<% @start = r.time_start < @fixfrom ? @fixfrom: r.time_start %>
		<% @end = r.time_end > @fixto ? @fixto : r.time_end  %>
		<% left = (@start-@fixfrom)*width_per_second + msie_pos_fix-1 %>
		<% left = left - ((@start-@from)/(24*60*60)).floor * width_per_second*60*60*8 %>
		<% right = (@end-@fixfrom)*width_per_second + msie_pos_fix-1 %>
		<% right = right - ((@end-@from)/(24*60*60)).floor * width_per_second*60*60*8 %>
		<% width = right-left + msie_width_fix +1 %>		
		
	   	style="width:<%=width %>px; top:<%=tops["a#{r.aircraft_id}"].to_i+1+msie_height_fix%>px;
	   	    left: <%=label_width+left%>px;
			background:<%= r.status=="approved"? (r.reservation_type == 'booking' ? 'lightgreen' : 'tan') : 'yellow' %>;
	   	">
	   	<%= can_see_names ? r.creator.full_name.gsub(" ","&nbsp;") : (@user.full_name == r.creator.full_name ? r.creator.full_name.gsub(" ","&nbsp;") : '&nbsp;') %></div>
		
	 	<%= if can_approve_reservations? then draggable_element("abar"+r.id.to_s , :revert => true) end %>
   	<% } %>
   	
    <% @instructor_reservations.each{|r|%>
    	<% if tops["i#{r.instructor_id}"].nil? then next end %> 
    	<div id="ibar<%=r.id%>" class="reservation_bar" 
				<% @start = r.time_start < @fixfrom ? @fixfrom: r.time_start %>
				<% @end = r.time_end > @fixto ? @fixto : r.time_end  %>
				<% left = (@start-@fixfrom)*width_per_second + msie_pos_fix-1 %>
				<% left = left - ((@start-@from)/(24*60*60)).floor * width_per_second*60*60*8 %>
				<% right = (@end-@fixfrom)*width_per_second + msie_pos_fix-1 %>
				<% right = right - ((@end-@from)/(24*60*60)).floor * width_per_second*60*60*8 %>
				<% width = right-left + msie_width_fix +1 %>
	   	style="width:<%=width %>px; top:<%=tops["i#{r.instructor_id}"].to_i+1+msie_height_fix%>px;
	   	    left: <%=label_width+left%>px;
		background:<%= r.status=="approved"? (r.reservation_type == 'booking' ? 'lightgreen' : 'tan') : 'yellow' %>;
   		">
	   	<%= can_see_names ? r.creator.full_name.gsub(" ","&nbsp;") : (@user.full_name == r.creator.full_name ? r.creator.full_name.gsub(" ","&nbsp;") : '&nbsp;') %></div>
   	<% } %>


<!-- sliding left panel -->
		<div id="left_bar" style="height:<%=graph_height%>px;width:100px;position:absolute; top:0px; left:0px; text-align:left;"  height=<%=graph_height%>>
		<% @aircrafts.each{|a|%>
    		<% if tops["a#{a.id}"].nil? then next end %>
		    <div style="background:lightblue;width:95px;max-width:95px;overflow:hidden;position:absolute;top:<%=tops['a'+a.id.to_s]%>px" class="aircraft_name">
		         [<%=a.default_office.name[0..0]%>]&nbsp;<%= a.identifier%>
		    </div>
		<% } %>
    	 <% @instructors.each{|i| %>
		    <div style="background:lightblue;width:95px;max-width:95px;max-height:20px;overflow:hidden;position:absolute;top:<%=tops['i'+i.id.to_s]%>px" class="aircraft_name">
		    [<%=i.default_office.name[0..0]%>]&nbsp;<%= i.full_name.gsub(' ','&nbsp;')%>
		    </div>
		<% } %>
        </div> 


<!-- sliding top panel -->

<div id="top_bar" style="position:absolute; left:0px; top:-32px;"> 
<% (0..@days-1).each{|x|%>
	<div class="vbar" id="vbar5<%=x%>" 
    style="background:lightblue;width:<%=width_per_day%>px;       left:<%=label_width+x*width_per_day+1%>px; 		text-align: center;">
		<%= (Date.new(@from.year,@from.month,@from.day)+x).to_time.strftime("%a, %b  %d")%>
    </div>

   <div class="vbar"  
    style=" top:15px;width:<%=width_per_hour*2+2%>px;
           left:<%=label_width+x*width_per_day%>px; background:#f0f5ff;
     		text-align: left;			border-left:1px solid black;
     		
           ">
		<%= @days < 5 ? 7: nil%>
    </div>


	<% (1..7).each{|i| %>
	   <div class="vbar" id="vbar3<%=x%>" 
	    style="top:15px;width:<%=width_per_hour*2+2%>px;
	           left:<%=label_width+x*width_per_day+width_per_hour*2*i%>px; background:<%= i%2==1 ? "#d0e5ff" : "#f0f5ff"%>;
	     		text-align: left;
	           ">
			<%= @days < 5 ? (i*2+7)%12: nil%>
	    </div>
	<%	} %>
<% } %>
</div>
   	
</div>

<% if session[:hidden_types].size > 0 %>
<%= link_to_remote 'Show Hidden Categories',
{:url=>{:controller=>'schedule',:action => 'unhide_types',:master=>@master_schedule},:update=>'master_schedule_wrap'}%>
<% end %>

<% if can_approve_reservations? || instructor?%>
    <% @aircraft_reservations.each{|r|%>
		<% if tops["a#{r.aircraft_id}"].nil? then next end %>
   	<div class="tooltip for_abar<%=r.id%>" style="width:300px;max-width:300px">
      <%=render_partial 'simple_reservation', :reservation => r%>
   	</div>
   	<% } %>
   	
    <% @instructor_reservations.each{|r|%>
    	<% if tops["i#{r.instructor_id}"].nil? then next end %> 
   	<div class="tooltip for_ibar<%=r.id%>" style="width:300px;max-width:300px">
      <%=render_partial 'simple_reservation', :reservation => r%>
   	</div>
   	<% } %>
   	
   	<script type="text/javascript" >
		var forceShift = true;
   	    Tooltip.setup();
   	</script>
<% end %>
 
   	<% if @master_schedule %>
	<div style="padding-left:80px">
	<table cellspacing="10px"><tr>
	<td style="text-align:center;width:200px"><div style="display:marker; height:20px;  background: lightgreen;"><label style="color:#001b57">Approved</label></div></td>
	<td style="text-align:center;width:200px"><div style="display:marker; height:20px; background: yellow;"><label style="color:#001b57">Awaiting Approval</label></div></td>
	<td style="text-align:center;width:200px"><div style="display:marker; height:20px; background: tan;"><label style="color:#001b57">Blocked</label></div></td>
	</tr></table>
	</div>
		<br/><br/>
		<%= link_to "Go back", :controller => "news", :action => "index" %>
	<% end %>


	<script type="text/javascript">
	    Calendar.setup({
	        inputField     :    "date",     // id of the input field
	        ifFormat       :    "%Y-%m-%d",     // format of the input field (even if hidden, this format will be honored)
	        button         :    "trigger",       
	        daFormat       :    "%Y-%m-%d",// format of the displayed date
	        align          :    "Tl",           // alignment (defaults to "Bl")
	        singleClick    :    true,
			onClose	   :   function(cal){ 
					cal.hide();
					new Ajax.Updater('master_schedule_wrap', '/schedule/master_schedule?'+Form.serialize('schedule_time_form')+'&master=<%=@master_schedule%>', {asynchronous:true, evalScripts:true});    
				 }
	    });
	</script>
